#include"sock.h"

void ini_path6(struct sockaddr_in6 * addr , struct in6_addr* in6addr , int port)
{
    memset(addr,0,sizeof(struct sockaddr_in6));
    addr->sin6_family = AF_INET6;
    addr->sin6_addr = *in6addr;
    addr->sin6_port = htons(port);
}

int main(int argc , char ** argv)
{
    if(argc < 3 || !strcmp("--help" , argv[1]))
        usageErr("%s host-address msg...\n", argv[0]);

    int sfd = socket(AF_INET6 , SOCK_DGRAM , 0);
    if(sfd == -1)
        errExit("socket");
    struct sockaddr_in6 addr;
    ini_path6(&addr,&in6addr_any,PORT_NUM);
    // argv[1] 是要建立的对等主机ip地址，将其转换为网络地址格式

    if(inet_pton(AF_INET6,argv[1] , &addr.sin6_addr) == -1)
        fatal("inet_pton failed for address %s\n",argv[1]);
    printf("%s\n",argv[1]);
    // printf("%s %s \n",addr.sin6_addr,argv[1]);
    char buf[BUF_SIZE];
    for(int i = 2;i < argc;++ i)
    {
        int len = strlen(argv[i]); // 发送到对等地址
        if(sendto(sfd,argv[i],len,0,(struct sockaddr *)&addr,sizeof(struct sockaddr_in6) ) == -1)
            errExit("sendto");
        int numBytes = recvfrom(sfd,buf,BUF_SIZE,0,NULL,NULL);
        if(numBytes == -1)
            errExit("recvform");
        printf("Response %d : %.*s\n",i - 1, numBytes,buf);
    }
    close(sfd);
    return 0;
    
}